<template>
  <div class="tabs-container">
    <div class="tabs-wrapper">
      <div v-for="tab in tabs" :key="tab.path" class="tab-item" :class="{ 'active': activeIndex === tab.path }"
        @click="switchTab(tab)" @contextmenu.prevent="openContextMenu(tab, $event)">
        <span>{{ tab.title }}</span>
        <el-icon class="close-icon" @click.stop="closeTab(tab, $event)">
          <Close />
        </el-icon>
      </div>
    </div>
    <!-- 右键菜单 -->
    <div v-show="contextMenu.visible" class="context-menu" :style="{
      left: contextMenu.x + 'px',
      top: contextMenu.y + 'px'
    }">
      <div class="menu-item" @click="closeLeft">关闭左侧</div>
      <div class="menu-item" @click="closeRight">关闭右侧</div>
      <div class="menu-item" @click="closeOthers">关闭其他</div>
      <div class="menu-item" @click="closeAll">全部关闭</div>
    </div>
  </div>

</template>

<script setup>
import { ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { Close } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()

// 新增右键菜单相关代码
const contextMenu = ref({
  visible: false,
  x: 0,
  y: 0,
  currentTab: null
})

const openContextMenu = (tab, event) => {
  contextMenu.value = {
    visible: true,
    x: event.clientX,
    y: event.clientY,
    currentTab: tab
  }
  document.addEventListener('click', closeContextMenu)
}

const closeContextMenu = () => {
  contextMenu.value.visible = false
  document.removeEventListener('click', closeContextMenu)
}

const closeLeft = () => {
  const index = tabs.value.findIndex(t => t.path === contextMenu.value.currentTab.path)
  tabs.value = tabs.value.filter((t, i) => i >= index || t.path === '/home')
  checkActiveTab()
}

const closeRight = () => {
  const index = tabs.value.findIndex(t => t.path === contextMenu.value.currentTab.path)
  tabs.value = tabs.value.filter((t, i) => i <= index || t.path === '/home')
  checkActiveTab()
}

const closeOthers = () => {
  tabs.value = tabs.value.filter(t =>
    t.path === '/home' || t.path === contextMenu.value.currentTab.path
  )
  checkActiveTab()
}

const closeAll = () => {
  tabs.value = tabs.value.filter(t => t.path === '/home')
  router.push('/home')
  activeIndex.value = '/home'
}

const checkActiveTab = () => {
  const exists = tabs.value.some(t => t.path === activeIndex.value)
  if (!exists) {
    const lastTab = tabs.value[tabs.value.length - 1]
    router.push(lastTab ? lastTab.fullPath : '/home')
  }
}

// 页签列表
const tabs = ref([])
// 当前激活页签
const activeIndex = ref('')

// 初始化首页
const initTabs = () => {
  const homeTab = {
    title: '首页',
    path: '/home'
  }
  tabs.value = [homeTab]
  activeIndex.value = '/home'
}

// 添加页签
const addTab = (route) => {
  const exists = tabs.value.some(tab => tab.path === route.path)
  if (!exists && route.meta?.showTab !== false) {
    tabs.value.push({
      title: route.meta?.title || route.name || '未命名',
      path: route.path,
      fullPath: route.fullPath
    })
  }
  activeIndex.value = route.path
}

// 切换页签
const switchTab = (tab) => {
  router.push(tab.fullPath)
}

// 关闭页签
const closeTab = (tab, e) => {
  e.stopPropagation()
  const index = tabs.value.findIndex(t => t.path === tab.path)
  tabs.value.splice(index, 1)

  if (tab.path === activeIndex.value) {
    const lastTab = tabs.value[tabs.value.length - 1]
    if (lastTab) {
      router.push(lastTab.fullPath)
    } else {
      router.push('/home')
    }
  }
}

// 监听路由变化
router.afterEach((to) => {
  addTab(to)
})

// 初始化
initTabs()
</script>

<style scoped>
.tabs-container {
  background: #f5f5f5;
  padding: 8px 16px 0;
  border-bottom: 1px solid #e8e8e8;
}

.tabs-wrapper {
  display: flex;
  overflow-x: auto;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
  transition: box-shadow 0.3s;
  position: relative;
}

.tab-item {
  display: flex;
  align-items: center;
  padding: 8px 20px;
  margin-right: 4px;
  background: #fff;
  border: 1px solid #d8dce5;
  border-bottom: none;
  cursor: pointer;
  transition: all 0.3s;
  user-select: none;
  transition: transform 0.2s, opacity 0.2s;
  position: relative;
}

.tab-item.active {
  color: #409eff;
  border-top: 2px solid #409eff;
  background: #f0f8ff;
}

.close-icon {
  margin-left: 8px;
  border-radius: 50%;
  padding: 2px;
}

.close-icon:hover {
  background: #c0c4cc;
  color: #fff;
}

/* 新增右键菜单样式 */
.context-menu {
  position: fixed;
  background: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  z-index: 9999;
  padding: 5px 0;
}

.menu-item {
  padding: 8px 20px;
  font-size: 14px;
  color: #606266;
  cursor: pointer;
  transition: all 0.3s;
}

.menu-item:hover {
  background: #f5f7fa;
  color: #409eff;
}

.tab-leave-active {
  position: absolute;
}

.tab-leave-to {
  opacity: 0;
  transform: translateY(-10px);
}
</style>
